<template>
  <a-layout style="padding: 24px">
    <a-layout-content
      :style="{
        background: '#fff',
        padding: '24px',
        margin: 0,
        minHeight: '400px'
      }"
    >
      <a-breadcrumb style="margin: 0 0 16px 0">
        <a-breadcrumb-item>首页</a-breadcrumb-item>
        <a-breadcrumb-item>系统管理</a-breadcrumb-item>
        <a-breadcrumb-item>用户管理</a-breadcrumb-item>
      </a-breadcrumb>
      <div class="header">
        <a-button type="primary">新建用户</a-button>
      </div>
      <a-table :data-source="userList" bordered :pagination="false">
        <a-table-column key="id" title="编号" data-index="id">
          <template slot-scope="id">
            <router-link :to="'/user/detail/' + id">{{ id }}</router-link>
          </template>
        </a-table-column>
        <a-table-column key="username" title="用户名" data-index="username" />
        <a-table-column key="nickname" title="昵称" data-index="nickname" />
        <a-table-column key="email" title="邮箱" data-index="email" />
        <a-table-column key="isAdmin" title="是否是管理员" data-index="isAdmin">
          <template slot-scope="text, record">
            <a-switch v-model="record.isAdmin"></a-switch>
          </template>
        </a-table-column>
        <a-table-column key="isActive" title="是否激活" data-index="isActive">
          <template slot-scope="text, record">
            <a-switch v-model="record.isActive"></a-switch>
          </template>
        </a-table-column>
      </a-table>
      <a-pagination
        v-model="current"
        :page-size-options="pageSizeOptions"
        :total="total"
        show-size-changer
        :page-size="pageSize"
        @change="getUserList"
        @showSizeChange="getUserList"
      >
        <template slot="buildOptionText" slot-scope="props">
          <span v-if="props.value !== '50'">{{ props.value }}条/页</span>
          <span v-if="props.value === '50'">全部</span>
        </template>
      </a-pagination>
    </a-layout-content>
  </a-layout>
</template>

<script>
export default {
  data() {
    return {
      userList: [],
      pageSizeOptions: ["5", "10", "20"],
      current: 1,
      pageSize: 10,
      total: 10
    };
  },
  mounted() {
    this.getUserList(1, 10);
  },
  methods: {
    getUserCount() {
      this.$get(`/user/count`).then(response => {
        if (response.code === 200) {
          this.total = response.data;
        }
      });
    },
    getUserList(page, rows) {
      this.$get(`/user?page=${page}&rows=${rows}`).then(response => {
        if (response.code === 200) {
          this.userList = response.data.map(item => {
            return { ...item, key: item["id"] };
          });
          this.current = page;
          this.pageSize = rows;
          this.getUserCount();
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.header {
  margin-bottom: 20px;
  height: 30px;
  button {
    float: left;
  }
}
.ant-pagination {
  margin-top: 20px;
  text-align: center;
}
</style>
